﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace WOWOGame
{
    public partial class Plume : UserControl
    {
        public Plume()
        {
            InitializeComponent();
            CreatePath();
            MyAngle.ScaleY = MyAngle.ScaleX = _random.NextDouble() + 0.3;            
        }
        static Random _random = new Random((int)DateTime.Now.Ticks);
        static double pi = 2 * Math.Acos((double)0);
        const int MaxPointNumber = 200;
        const double N = 2;
        const double h = 20;
        private Point[] pathPointGroup;
        private void CreatePath()
        {
            int aw = (int)App.Current.Host.Content.ActualWidth;
            int ah = (int)App.Current.Host.Content.ActualHeight / 5 * 4;
            double l;
            double x1, y1, x2, y2;
            x1 = _random.Next((int)App.Current.Host.Content.ActualWidth / 2 - 100, (int)App.Current.Host.Content.ActualWidth / 2 + 100);
            y1 = _random.Next(10,100);
            x2 = _random.Next(0, aw);
            y2 = _random.Next(ah/4, ah); 
            pathPointGroup = new Point[MaxPointNumber];
            for (double i = 1; i <= MaxPointNumber; i = i + 1)
            {
                l = Math.Sqrt(Math.Pow(x2 - x1, 2) + Math.Pow(y2 - y1, 2));
                double x, y;
                x = (i / MaxPointNumber * l * (x2 - x1) - (y2 - y1) * h * Math.Sin(i / MaxPointNumber * 2 * pi * N)) / l + x1;
                y = (h * Math.Sin(i / MaxPointNumber * 2 * pi * N) * (x2 - x1) + (y2 - y1) * i / MaxPointNumber * l) / l + y1;
                pathPointGroup[(int)i-1] = new Point(x, y);
            }
            IsRun = true;
            index = _random.Next(0, MaxPointNumber);
        }
        public bool IsRun = false;
        private int index = 0;
        public void OnFrame()
        {
            if (IsRun)
            {
                index++;
                if (index >= MaxPointNumber)
                    index = 0;
                Point pt = pathPointGroup[index];
                Canvas.SetLeft(ellipse, pt.X);
                Canvas.SetTop(ellipse, pt.Y);
                ellipse.Opacity = 1 - ((double)index / (double)MaxPointNumber);
                MyAngle.Rotation = ellipse.Opacity * 360;
            }
        }
    }
}
